/**
 * Copyright 2011 classic-commerce
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package ca.ioniq.server.service.impl;

import java.util.List;

import javax.inject.Inject;

import ca.ioniq.server.dao.BrandDAO;
import ca.ioniq.server.dao.CategoryDAO;
import ca.ioniq.server.dao.ItemDAO;
import ca.ioniq.server.service.InventoryService;
import ca.ioniq.server.service.exception.InventoryException;
import ca.ioniq.shared.entity.Brand;
import ca.ioniq.shared.entity.Category;
import ca.ioniq.shared.entity.Item;

import com.google.inject.persist.Transactional;

public class InventoryServiceImpl implements InventoryService {

    private final ItemDAO itemDao;
    private final BrandDAO brandDao;
    private final CategoryDAO categoryDao;

    @Inject
    public InventoryServiceImpl(ItemDAO itemDao, BrandDAO brandDao, CategoryDAO categoryDao) {
        this.itemDao = itemDao;
        this.brandDao = brandDao;
        this.categoryDao = categoryDao;
    }

	@Transactional
    public void updateItem(Item item, long itemId) throws InventoryException {
        try {
            Item persistedItem = itemDao.findById(itemId);
            persistedItem = item;
            itemDao.update(persistedItem);
        } catch (Exception e) {
            throw new InventoryException("Cannot remove this item; maybe this item is not present in repository", e);
        }
    }

	@Transactional
    public Item addItem(Item item) {
        itemDao.persist(item);
        return item;
    }

	@Transactional
    public void removeItem(Item item) throws InventoryException {
        try {
            itemDao.delete(item);
        } catch (Exception e) {
            throw new InventoryException("Cannot remove this item; maybe this item is not present in repository", e);
        }
    }

	@Transactional
    public void removeItem(long itemId) throws InventoryException {
        try {
            Item persistedItem = itemDao.findById(itemId);
            itemDao.delete(persistedItem);
        } catch (Exception e) {
            throw new InventoryException("Cannot remove this item; maybe this item is not present in repository", e);
        }
    }

	@Transactional
    public void updateCategory(Category category, long categoryId) throws InventoryException {
        try {
            Category persistedCategory = categoryDao.findById(categoryId);
            persistedCategory = category;
            categoryDao.update(persistedCategory);
        } catch (Exception e) {
            throw new InventoryException("Cannot remove this category; maybe this category is not present in repository", e);
        }
    }

	@Transactional
    public Category addCategory(Category category) {
        categoryDao.persist(category);
        return category;
    }

	@Transactional
    public void removeCategory(Category category) throws InventoryException {
        try {
            categoryDao.delete(category);
        } catch (Exception e) {
            throw new InventoryException("Cannot remove this category; maybe this category is not present in repository", e);
        }
    }

	@Transactional
    public void removeCategory(long categoryId) throws InventoryException {
        try {
            Category persistedCategory = categoryDao.findById(categoryId);
            categoryDao.delete(persistedCategory);
        } catch (Exception e) {
            throw new InventoryException("Cannot remove this category; maybe this category is not present in repository", e);
        }
    }

	@Transactional
    public void updateBrand(Brand brand, long brandId) throws InventoryException {
        try {
            Brand persistedBrand = brandDao.findById(brandId);
            persistedBrand = brand;
            brandDao.update(persistedBrand);
        } catch (Exception e) {
            throw new InventoryException("Cannot remove this brand; maybe this brand is not present in repository", e);
        }
    }

	@Transactional
    public Brand addBrand(Brand brand) {
        brandDao.persist(brand);
        return brand;
    }

	@Transactional
    public void removeBrand(Brand brand) throws InventoryException {
        try {
            brandDao.delete(brand);
        } catch (Exception e) {
            throw new InventoryException("Cannot remove this brand; maybe this brand is not present in repository", e);
        }
    }

	@Transactional    
    public void removeBrand(long brandId) throws InventoryException {
        try {
            Brand persistedBrand = brandDao.findById(brandId);
            brandDao.delete(persistedBrand);
        } catch (Exception e) {
            throw new InventoryException("Cannot remove this brand; maybe this brand is not present in repository", e);
        }
    }

	@Transactional
	public List<Item> getItemsByBrand(Long brandId) {
		return itemDao.getItemsByBrand(brandId);
	}

	@Transactional
	public List<Item> getItemsByCategoryAndCompany(Long categoryId, Long companyId) {
		return itemDao.getItemsByCategoryAndCompany(categoryId, companyId);

	}

}
